package com.ellecity06.common.ktx

import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.annotation.ColorRes
import androidx.annotation.DimenRes
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.ellecity06.common.R
import com.ellecity06.common.widgets.decoration.NormalGridDecoration
import com.ellecity06.common.widgets.decoration.NormalLinearDecoration
import com.scwang.smart.refresh.layout.SmartRefreshLayout

/**
 * @author ellecity06
 * @time 2023/4/17 15:38
 * @des
 */
/**
 * 清除RecyclerView的动画
 */
fun RecyclerView.clearAnimations() {
    val itemAnimator = this.itemAnimator
    itemAnimator?.addDuration = 0
    itemAnimator?.changeDuration = 0
    itemAnimator?.moveDuration = 0
    itemAnimator?.removeDuration = 0
    if (itemAnimator is SimpleItemAnimator) itemAnimator.supportsChangeAnimations = false

}

fun RecyclerView.addGridDecoration(
    @DimenRes size: Int = R.dimen.dp_10,
    @ColorRes color: Int = R.color.transparent
) {
    if (itemDecorationCount > 0) {
        val itemDecorationAt = getItemDecorationAt(0)
        removeItemDecoration(itemDecorationAt)
        addItemDecoration(NormalGridDecoration(size = size, color = color))
    } else {
        addItemDecoration(NormalGridDecoration(size = size, color = color))
    }

}

fun RecyclerView.addLinearDecoration(
    @DimenRes size: Int = R.dimen.dp_10,
    @ColorRes color: Int = R.color.transparent
) {
    if (itemDecorationCount > 0) {
        val itemDecorationAt = getItemDecorationAt(0)
        removeItemDecoration(itemDecorationAt)
        addItemDecoration(NormalLinearDecoration(size = size, color = color))
    } else {
        addItemDecoration(NormalLinearDecoration(size = size, color = color))
    }

}

fun RecyclerView.setMargins(left: Int, top: Int, right: Int, bottom: Int) {
    when (val lp = layoutParams) {
        is SmartRefreshLayout.LayoutParams -> {
            lp.setMargins(left, top, right, bottom)
            layoutParams = lp
        }
        is FrameLayout.LayoutParams -> {
            lp.setMargins(left, top, right, bottom)
            layoutParams = lp
        }
    }

}